<?php
class TransactionsController extends AppController
{
    public $name = 'Transactions';
    public function beforeFilter()
    {
        $this->Security->disabledFields = array(
            'Transaction.user_id',
        );
        parent::beforeFilter();
    }
    public function admin_index()
    {
        $this->_redirectGET2Named(array(
            'user_id',
            'username',
            'project_id',
        ));
        $this->Transaction->User->validate = array();
		$this->Transaction->Project->validate = array();
		$conditions = array();
        $this->pageTitle = __l('Transactions');
        if (empty($this->request->named['user_id']) && !empty($this->request->named['username'])) {
			$user = $this->Transaction->User->find('first', array(
                'conditions' => array(
                    'User.username' => $this->request->named['username'],
                    'ProjectFund.is_failed' => 0
                ) ,
                'fields' => array(
                    'User.id'
                ) ,
                'recursive' => -1
            ));
            if (!empty($user)) {
                $conditions['Transaction.user_id'] = $user['User']['id'];
            } else {
                $this->request->named['user_id'] = null;
            }
        }
        if((!empty($this->request->named['filter']) && $this->request->named['filter'] =='admin') || empty($this->request->named['filter'])){
        	$conditions['Transaction.user_id'] = ConstUserIds::Admin;
        }
        if((!empty($this->request->named['filter']) && $this->request->named['filter'] =='all')){

        }
        if(!empty($this->request->named['user_id'])){
        	$conditions['Transaction.user_id'] = $this->request->named['user_id'];
        }
        if (!empty($this->request->named['project_id'])) {
			$conditions['Transaction.foreign_id'] = $this->request->named['project_id'];
			$conditions['Transaction.class'] = 'Project';
        }
		if (!empty($this->request->params['named']['type']) && $this->request->params['named']['type'] == 'signup_fee') {
            $conditions['Transaction.class'] = 'User';
        } elseif (!empty($this->request->params['named']['type']) && $this->request->params['named']['type'] == 'listing_fee') {
            $conditions['Transaction.class'] = 'Project';
        }
        if (!empty($this->request->params['named']['stat']) && $this->request->params['named']['stat'] == 'day') {
            $conditions['TO_DAYS(NOW()) - TO_DAYS(Transaction.created) <= '] = 0;
            $this->pageTitle.= __l(' - today');
        }
        if (!empty($this->request->params['named']['stat']) && $this->request->params['named']['stat'] == 'week') {
            $conditions['TO_DAYS(NOW()) - TO_DAYS(Transaction.created) <= '] = 7;
            $this->pageTitle.= __l(' - in this week');
        }
        if (!empty($this->request->params['named']['stat']) && $this->request->params['named']['stat'] == 'month') {
            $conditions['TO_DAYS(NOW()) - TO_DAYS(Transaction.created) <= '] = 30;
            $this->pageTitle.= __l(' - in this month');
        }
        $this->paginate = array(
            'conditions' => $conditions,
            'contain' => array(
                'Project' => array(
                    'User' => array(
                        'fields' => array(
                            'User.id',
                            'User.username'
                        )
                    ) ,
                    'fields' => array(
                        'Project.id',
                        'Project.user_id',
                        'Project.name',
                        'Project.slug'
                    )
                ) ,
                'ProjectFund' => array(
                    'Project' => array(
                        'fields' => array(
                            'Project.id',
                            'Project.user_id',
                            'Project.name',
                            'Project.slug',
                            'Project.project_status_id',
                        ),
                    'User' => array(
                        'fields' => array(
                            'User.id',
                            'User.username',
                            'User.user_type_id'
                        )
                    )
                    ) ,
                    'User' => array(
                        'fields' => array(
                            'User.id',
                            'User.username',
                            'User.user_type_id'
                        )
                    ) ,
                    'fields' => array(
                        'ProjectFund.id',
                        'ProjectFund.user_id',
                        'ProjectFund.project_id',
                        'ProjectFund.site_fee',
                        'ProjectFund.amount'
                    )
                ) ,
                'User' => array(
                    'UserAvatar',
                    'fields' => array(
                        'User.id',
                        'User.username',
                        'User.user_type_id'
                    )
                ) ,
                'ForeignUser' => array(
                        'fields' => array(
                        'ForeignUser.id',
                        'ForeignUser.username',
                        'ForeignUser.user_type_id',
                    )
                    
                ) ,
                'TransactionType' => array(
                    'fields' => array(
                        'TransactionType.id',
                        'TransactionType.name',
						'TransactionType.is_credit',
						'TransactionType.message',
						'TransactionType.message_for_admin'
                    )
                )
            ) ,
            'order' => array(
                'Transaction.id' => 'desc'
            ) ,
            'recursive' => 3
        );
        $projects = array();
		$projects = $this->Transaction->Project->find('list', array(
            'conditions' => array(
                'Project.is_active' => 1
            ) ,
			'fields' => array(
                'Project.id',
                'Project.name',
            ),
			'recursive' => -1,
        ));
        $this->set('projects',$projects);
        $this->set('transactions', $this->paginate());
    }
	  public function index()
    {
		$this->_redirectPOST2Named(array(
            'Transaction.from_date.year',
            'Transaction.from_date.month',
			'Transaction.from_date.day',
			'Transaction.to_date.year',
            'Transaction.to_date.month',
			'Transaction.to_date.day'
        ));
	
        $this->pageTitle = __l('Transactions');
        $conditions['Transaction.user_id'] = $this->Auth->user('id');
        
        $blocked_conditions['UserCashWithdrawal.user_id'] = $this->Auth->user('id');
        /*if (isset($this->request->data['Transaction']['from_date']) and isset($this->request->data['Transaction']['to_date'])) {
            $from_date = $this->request->data['Transaction']['from_date']['year'] . '-' . $this->request->data['Transaction']['from_date']['month'] . '-' . $this->request->data['Transaction']['from_date']['day'] . ' 00:00:00';
            $to_date = $this->request->data['Transaction']['to_date']['year'] . '-' . $this->request->data['Transaction']['to_date']['month'] . '-' . $this->request->data['Transaction']['to_date']['day'] . ' 23:59:59';
			
        }
        if (!empty($this->request->data)) {
            if ($from_date < $to_date) {
                $blocked_conditions['UserCashWithdrawal.created >='] = $conditions['Transaction.created >='] = $from_date;
                $blocked_conditions['UserCashWithdrawal.created <='] = $conditions['Transaction.created <='] = $to_date;
            } else {
                $this->Session->setFlash(__l('From date should less than To date. Please, try again.') , 'default', null, 'error');
            }
        }*/
			if(isset($this->request->params['named']['Transaction.from_date.year']) && isset($this->request->params['named']['Transaction.from_date.month']) && isset($this->request->params['named']['Transaction.from_date.day']) && isset($this->request->params['named']['Transaction.to_date.year']) && isset($this->request->params['named']['Transaction.to_date.month']) && isset($this->request->params['named']['Transaction.to_date.day']))
		{
			$from_date = $this->request->params['named']['Transaction.from_date.year'] . '-' . $this->request->params['named']['Transaction.from_date.month'] . '-' . $this->request->params['named']['Transaction.from_date.day'] . ' 00:00:00';
			
			$to_date = $this->request->params['named']['Transaction.to_date.year'] . '-' . $this->request->params['named']['Transaction.to_date.month'] . '-' . $this->request->params['named']['Transaction.to_date.day'] .' 23:59:59';
			
			$blocked_conditions['UserCashWithdrawal.created >='] = $conditions['Transaction.created >='] = $from_date;
            $blocked_conditions['UserCashWithdrawal.created <='] = $conditions['Transaction.created <='] = $to_date;
		}
        if (isset($this->request->params['named']['stat']) && $this->request->params['named']['stat'] == 'day') {
            $conditions['TO_DAYS(NOW()) - TO_DAYS(Transaction.created) <= '] = 0;
            $blocked_conditions['TO_DAYS(NOW()) - TO_DAYS(UserCashWithdrawal.created) <= '] = 0;
            $this->pageTitle.= __l(' - Amount Earned today');
            $this->request->data['Transaction']['from_date'] = array(
                'year' => date('Y', strtotime('today')) ,
                'month' => date('m', strtotime('today')) ,
                'day' => date('d', strtotime('today'))
            );
            $this->request->data['Transaction']['to_date'] = array(
                'year' => date('Y', strtotime('today')) ,
                'month' => date('m', strtotime('today')) ,
                'day' => date('d', strtotime('today'))
            );
        }
        if (isset($this->request->params['named']['stat']) && $this->request->params['named']['stat'] == 'week') {
            $conditions['TO_DAYS(NOW()) - TO_DAYS(Transaction.created) <= '] = 7;
            $blocked_conditions['TO_DAYS(NOW()) - TO_DAYS(UserCashWithdrawal.created) <= '] = 7;
            $this->pageTitle.= __l(' - Amount Earned in this week');
            $this->request->data['Transaction']['from_date'] = array(
                'year' => date('Y', strtotime('last week')) ,
                'month' => date('m', strtotime('last week')) ,
                'day' => date('d', strtotime('last week'))
            );
            $this->request->data['Transaction']['to_date'] = array(
                'year' => date('Y', strtotime('this week')) ,
                'month' => date('m', strtotime('this week')) ,
                'day' => date('d', strtotime('this week'))
            );
        }
        if (isset($this->request->params['named']['stat']) && $this->request->params['named']['stat'] == 'month') {
            $conditions['TO_DAYS(NOW()) - TO_DAYS(Transaction.created) <= '] = 30;
            $blocked_conditions['TO_DAYS(NOW()) - TO_DAYS(UserCashWithdrawal.created) <= '] = 30;
            $this->pageTitle.= __l(' - Amount Earned in this month');
            $this->request->data['Transaction']['from_date'] = array(
                'year' => date('Y', (strtotime('last month', strtotime(date('m/01/y'))))) ,
                'month' => date('m', (strtotime('last month', strtotime(date('m/01/y'))))) ,
                'day' => date('d', (strtotime('last month', strtotime(date('m/01/y')))))
            );
            $this->request->data['Transaction']['to_date'] = array(
                'year' => date('Y', (strtotime('this month', strtotime(date('m/01/y'))))) ,
                'month' => date('m', (strtotime('this month', strtotime(date('m/01/y'))))) ,
                'day' => date('d', (strtotime('this month', strtotime(date('m/01/y')))))
            );
        }
		
        $this->paginate = array(
            'conditions' => $conditions,
            'contain' => array(
                'Project' => array(
                    'User' => array(
                        'fields' => array(
                            'User.id',
                            'User.username'
                        )
                    ) ,
                    'fields' => array(
                        'Project.id',
                        'Project.user_id',
                        'Project.name',
                        'Project.slug'
                    )
                ) ,
                'ProjectFund' => array(
                   'conditions' => array('ProjectFund.is_failed' => 0),
                    'Project' => array(
                        'fields' => array(
                            'Project.id',
                            'Project.user_id',
                            'Project.name',
                            'Project.slug',
                            'Project.project_status_id',
                        ),
                    'User' => array(
                        'fields' => array(
                            'User.id',
                            'User.username',
                            'User.user_type_id'
                        )
                    )
                    ) ,
                    'User' => array(
                        'fields' => array(
                            'User.id',
                            'User.username',
                            'User.user_type_id'
                        )
                    ) ,
                    'fields' => array(
                        'ProjectFund.id',
                        'ProjectFund.user_id',
                        'ProjectFund.project_id',
                        'ProjectFund.site_fee',
                        'ProjectFund.amount'
                    )
                ) ,
                'User' => array(
                    'UserAvatar',
                    'fields' => array(
                        'User.id',
                        'User.username',
                        'User.user_type_id'
                    )
                ) ,
                'ForeignUser' => array(
                        'fields' => array(
                        'ForeignUser.id',
                        'ForeignUser.username',
                        'ForeignUser.user_type_id',
                    )

                ) ,
                'TransactionType' => array(
                    'fields' => array(
                        'TransactionType.id',
                        'TransactionType.name',
						'TransactionType.is_credit',
						'TransactionType.message',
						//'TransactionType.message_for_admin'
                    )
                )
            ) ,
            'order' => array(
                'Transaction.id' => 'desc'
            ) ,
            'recursive' => 3
        );
        $transactions = $this->paginate();
        $this->set('transactions', $transactions);
        // To get commission percentage
        $credit = $this->Transaction->find('first', array(
            'conditions' => array(
                $conditions,
            ) ,
            'fields' => array(
                'SUM(Transaction.amount) as total_amount'
            ) ,
            'group' => array(
                'Transaction.user_id'
            ) ,
            'recursive' => 0
        ));
        $this->set('total_credit_amount', !empty($credit[0]['total_amount']) ? $credit[0]['total_amount'] : 0);
        $debit = $this->Transaction->find('first', array(
            'conditions' => array(
                $conditions,
                'TransactionType.is_credit' => 0
            ) ,
            'fields' => array(
                'SUM(Transaction.amount) as total_amount'
            ) ,
            'group' => array(
                'Transaction.user_id'
            ) ,
            'recursive' => 0
        ));
        $from = $this->Transaction->find('first', array(
            'conditions' => $conditions,
            'fields' => array(
                'Transaction.created'
            ) ,
            'limit' => 1,
            'recursive' => -1
        ));
        $to = $this->Transaction->find('first', array(
            'conditions' => $conditions,
            'fields' => array(
                'Transaction.created'
            ) ,
            'limit' => 1,
            'order' => array(
                'Transaction.id desc'
            ) ,
            'recursive' => -1
        ));
        $this->set('duration_from', $from['Transaction']['created']);
        $this->set('duration_to', $to['Transaction']['created']);
        $this->set('total_debit_amount', !empty($debit[0]['total_amount']) ? $debit[0]['total_amount'] : 0);
        $blocked_amount = $this->Transaction->User->UserCashWithdrawal->find('first', array(
            'conditions' => $blocked_conditions,
            'fields' => array(
                'SUM(UserCashWithdrawal.amount) as total_amount'
            ) ,
            'group' => array(
                'UserCashWithdrawal.user_id'
            ) ,
            'recursive' => 0
        ));
        $filter = array(
            'all' => __l('All') ,
            'day' => __l('Today') ,
            'week' => __l('This Week') ,
            'month' => __l('This Month') ,
            'custom' => __l('Custom') ,
        );
        if ($this->RequestHandler->isAjax()) {
            $this->set('isAjax', true);
        } else {
            $this->set('isAjax', false);
        }
		 $credit = $this->Transaction->find('first', array(
            'conditions' => array(
                $conditions,
                'TransactionType.is_credit' => 1
            ) ,
            'fields' => array(
                'SUM(Transaction.amount) as total_amount'
            ) ,
            'group' => array(
                'Transaction.user_id'
            ) ,
            'recursive' => 0
        ));
        $this->set('total_credit_amount', !empty($credit[0]['total_amount']) ? $credit[0]['total_amount'] : 0);
        $debit = $this->Transaction->find('first', array(
            'conditions' => array(
                $conditions,
                'TransactionType.is_credit' => 0
            ) ,
            'fields' => array(
                'SUM(Transaction.amount) as total_amount'
            ) ,
            'group' => array(
                'Transaction.user_id'
            ) ,
            'recursive' => 0
        ));
        $this->set('filter', $filter);
        $this->set('blocked_amount', !empty($blocked_amount[0]['total_amount']) ? $blocked_amount[0]['total_amount'] : 0);
    }
    public function admin_delete($id = null)
    {
        if (is_null($id)) {
            throw new NotFoundException(__l('Invalid request'));
        }
        if ($this->Transaction->delete($id)) {
            $this->Session->setFlash(__l('Transaction deleted') , 'default', null, 'success');
            $this->redirect(array(
                'action' => 'index'
            ));
        } else {
            throw new NotFoundException(__l('Invalid request'));
        }
    }
}
?>